package com.authine.cloudpivot.web.Utils;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
    private static DateFormat datafFormat = new SimpleDateFormat("MMdd");

    // 每周开始结束时间
    public static Map<Integer, WeekRange> getWeekBeginAndEnd(Date startDate, Date endDate) {
        startDate.setSeconds(0);
        startDate.setMinutes(0);
        startDate.setHours(0);
        endDate.setSeconds(0);
        endDate.setMinutes(0);
        endDate.setHours(0);
        Map<Integer, WeekRange> maps = new HashMap<Integer, WeekRange>();
        getWeekBeginAndEnd(0,startDate,endDate,maps);
        return maps;
    }
    // 星期几
    public static int week(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) -1 ;
    }

    // 下一天
    public static Date nextDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    // 每周开始结束时间
    public static void getWeekBeginAndEnd(int index,Date currentDate, Date lastMonthDate, Map<Integer, WeekRange> maps){
        int week = week(currentDate);
        if(null == maps){
            WeekRange range = new WeekRange(currentDate, currentDate);
            maps = new HashMap<Integer, WeekRange>();
            maps.put(index,range);
        }else{
            WeekRange range = maps.get(index);
            if(null == range){
                range = new WeekRange(currentDate);
            }
            if(range.begin.getMonth() != currentDate.getMonth()){
                index++;
                getWeekBeginAndEnd(index,currentDate,lastMonthDate,maps);
            }else {
                range.setEnd(currentDate);
                maps.put(index, range);
            }
        }

        if(currentDate.equals(lastMonthDate)){
            return;
        }

        if(week == 0){
            index++;
        }

        getWeekBeginAndEnd(index,nextDate(currentDate),lastMonthDate,maps);
    }

    public static String format(Date date) {
        return datafFormat.format(date);

    }


    public static class WeekRange{
        Date begin;//周开始日
        Date end;//周结束日
        public WeekRange(Date begin) {
            super();
            this.begin = begin;
        }
        public WeekRange(Date begin, Date end) {
            super();
            this.begin = begin;
            this.end = end;
        }
        public Date getBegin() {
            return begin;
        }
        public void setBegin(Date begin) {
            this.begin = begin;
        }
        public Date getEnd() {
            return end;
        }
        public void setEnd(Date end) {
            this.end = end;
        }

        @Override
        public String toString() {
            if(this.begin.equals(this.end)){
                return format(this.begin);
            }else{
                return  format(this.begin) +"-"+format(this.end);
            }
        }
    }
}
